package com.ogawa.project8506e.fragment

import android.view.View
import com.google.gson.Gson
import com.ogawa.base.base.BaseFragment
import com.ogawa.base.constants.Constants
import com.ogawa.project8506e.R
import com.ogawa.project8506e.activity.Device8506eDIYActivity
import com.ogawa.project8506e.bean.Massage8506EArmchair
import com.ogawa.projectcommon.bean.*
import com.ogawa.projectcommon.constants.Constant
import com.ogawa.projectcommon.utils.ProjectSpUtils
import com.ogawa.projectcommon.widget.DIYChartWidget2
import com.ogawa.projectcommon.widget.TimeRange
import com.ogawa.projectcommon.widget.TimeRangeView
import kotlinx.android.synthetic.main.fragment_d_i_y8506e.*

/**
 *
 */
class DIY8506eFragment(var param1: String?, val param2: String?) : BaseFragment(),
    DIYChartWidget2.OnAreaClickListener {
    private var diyBean: DiyBean? = null
    private var setDiyDataComplete = false

    override fun getLayoutId(): Int {
        return R.layout.fragment_d_i_y8506e
    }

    override fun initView(rootView: View) {
        diyChartWidget.setAreaClickListener(this)
    }

    fun setDiyContent(content: DiyBean) {
        diyBean = content
        if (isResumed) {
            setDiyData()
        }
    }

    override fun onResume() {
        super.onResume()
        if (!setDiyDataComplete) {
            setDiyData()
        }
    }

    private fun setDiyData() {
        if (diyBean != null && !diyBean!!.content.isNullOrEmpty()) {
            val diyContentBean = Gson().fromJson(diyBean?.content, DiyContentBean::class.java)
            diyChartWidget.setDiyData(
                diyContentBean.movements,
                diyContentBean.airs,
                diyContentBean.hots,
                diyContentBean.rollers
            )
            setDiyDataComplete = true
        }
    }

    fun getDiyBean(share: Boolean = false): DiyBean? {
        val movementBeans = diyChartWidget.movementBeans
        val gasBeans = diyChartWidget.gasBeans
        val heatBeans = diyChartWidget.heatBeans
        val rollBeans = diyChartWidget.rollBeans
        val diyContentBean = DiyContentBean(movementBeans, gasBeans, heatBeans, rollBeans)
        val content: String = Gson().toJson(diyContentBean)
        if (!share) {//不是分享，即保存，加工后再返回，否则直接返回
            if (diyBean != null) {
                diyBean!!.content = content
                diyBean!!.typeCode = ProjectSpUtils.getCurDeviceTypeCode()
                diyBean!!.platform = Constants.NET_DEF_PLATFORM
            } else {
                diyBean =
                    DiyBean(
                        ProjectSpUtils.getCurDeviceTypeCode(),
                        "",
                        content,
                        param1!!.toInt(),
                        param1?.toInt()
                    )
            }
        }
        return diyBean!!
    }

    fun addMovement(movementBean: MovementBean) {
        diyChartWidget.addMovementBean(movementBean)
    }

    fun addGas(gasBean: GasBean) {
        diyChartWidget.addGasBean(gasBean)
    }

    fun addHeat(heatBean: HeatRollBean) {
        diyChartWidget.addHeatBean(heatBean)
    }

    fun addRoll(rollBean: HeatRollBean) {
        diyChartWidget.addRollBean(rollBean)
    }

    fun refreshView() {
        diyChartWidget.postInvalidate()
    }

    fun clearDiyData() {
        diyChartWidget.clearData()
    }

    fun setTimeRange(type: Int, trv: TimeRangeView, excludeRange: TimeRange? = null) {
        trv.clearTimeRange()
        when (type) {
            0 -> {
                for (movementBean in diyChartWidget.movementBeans) {
                    if (movementBean.startTime != excludeRange?.start && movementBean.endTime != excludeRange?.end) {
                        trv.setTimeRange(TimeRange(movementBean.startTime, movementBean.endTime))
                    }
                }
            }
            1 -> {
                for (movementBean in diyChartWidget.gasBeans) {
                    if (movementBean.startTime != excludeRange?.start && movementBean.endTime != excludeRange?.end) {
                        trv.setTimeRange(TimeRange(movementBean.startTime, movementBean.endTime))
                    }
                }
            }
            2 -> {
                for (movementBean in diyChartWidget.heatBeans) {
                    if (movementBean.startTime != excludeRange?.start && movementBean.endTime != excludeRange?.end) {
                        trv.setTimeRange(TimeRange(movementBean.startTime, movementBean.endTime))
                    }
                }
            }
            3 -> {
                for (movementBean in diyChartWidget.rollBeans) {
                    if (movementBean.startTime != excludeRange?.start && movementBean.endTime != excludeRange?.end) {
                        trv.setTimeRange(TimeRange(movementBean.startTime, movementBean.endTime))
                    }
                }
            }
        }
    }

    fun getDiyCommand(): String {
        return diyChartWidget.getTotalCommand("0$param1")
    }

    fun setWidgetTouchEnable(touchable: Boolean) {
        diyChartWidget?.isTouchable = touchable
    }

    fun onDeviceStateChange() {
        val deviceStatusBean = Massage8506EArmchair.getInstance().deviceStatusBean
        val diyProgramCurrentStep = deviceStatusBean.functions.diyProgramCurrentStep
        val diyProgramKey = deviceStatusBean.functions.diyProgramKey
        if (diyProgramKey.modelValue != param1) {
            diyChartWidget?.curTime = -1
            return
        }
        diyChartWidget?.curTime = diyProgramCurrentStep.modelValue.toInt()
    }

    override fun onAreaClick(listIndex: Int, areaListIndex: Int) {
        when (listIndex) {
            0 -> {
                (activity as Device8506eDIYActivity).showJxEditDialog(diyChartWidget.movementBeans[areaListIndex])
            }
            1 -> {
                (activity as Device8506eDIYActivity).showGasEditDialog(diyChartWidget.gasBeans[areaListIndex])
            }
            2 -> {
                (activity as Device8506eDIYActivity).showHeatEditDialog(diyChartWidget.heatBeans[areaListIndex])
            }
            3 -> {
                (activity as Device8506eDIYActivity).showRollEditDialog(diyChartWidget.rollBeans[areaListIndex])
            }
        }
    }
}